// ajax 封装
// 判断当前开发环境 设置公共请求头
//https://crm.jxscloudmedia.com/index.php
//http://192.168.1.104/jxkeda_oa/public/index.php
const baseUrl = process.env.NODE_ENV === 'development' ? 'https://oajxkjzy.ncymkj.com/index.php' :
	'https://oajxkjzy.ncymkj.com/index.php'
// token验证接口 
const tokenApi = '/api/admin/checkToken'
// 登录页面路由地址
const loginUrl = '/pages/login/login';
// 设置一个不需要登录弹窗提示的路由地址数组
let routerArr = ['pages/proFile/proFile', 'pages/index/index']
// get请求
export const getRequest = (url, data = {}) => {
	uni.showLoading({
		title: "loading",
		mask: true
	})
	return new Promise((resolve, reject) => {
		// data.token = uni.getStorageSync('token')
		uni.request({
			url: baseUrl + url,
			data: data,
			method: 'GET',
			header: {
				'content-type': 'application/json',
			},
			success: res => {
				if (res.statusCode != 200) {
					uni.showModal({
						title: '错误警告',
						content: `出现${res.statusCode}错误`,
						showCancel: false
					})
					return
				}
				uni.hideLoading();
				resolve(res.data)

			},
			fail: res => {
				uni.hideLoading();
				return reject(res.data)
			}
		})
	})

}
// post 请求
export const postRequestG = (url, data = {}, blon = true, loading = true) => {
	if (loading) {
		uni.showLoading({
			title: "loading",
			mask: true
		})
	}
	if (blon) {
		data.token = uni.getStorageSync('token')
		// 验证token
		return new Promise((resolve, reject) => {
			data.hasOwnProperty('is_need_token') && (delete data.is_need_token,
				delete data.token)
			uni.request({
				url: baseUrl + url,
				data: data,
				method: 'GET',
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'token': data.token
				},
				success: result => {
					if (result.statusCode != 200) {
						uni.showModal({
							title: '错误警告',
							content: `出现${result.statusCode}错误`,
							showCancel: false
						})
						return
					}
					if (result.data.code == 10003) {
						// uni.clearStorage()
						let pathArr = getCurrentPages();
						let nowPath = pathArr[pathArr.length - 1].route
						if (routerArr.includes(nowPath)) {
							return
						}
						uni.showModal({
							title: "登录",
							content: "您还未登录或登录已过期,请重新登录~",
							showCancel: true,
							success: res => {
								if (res.confirm) {
									uni.navigateTo({
										url: loginUrl
									})
								} else {
									let path = getCurrentPages()
									if (path.length > 1) {
										uni.navigateBack({
											delta: 1,
											fail(error) {
												uni.reLaunch({
													url: '/pages/index/index'
												})
											}
										})
										// uni.navigateBack()
									} else {
										uni.reLaunch({
											url: '/pages/index/index'
										})
									}
								}
							}

						})
					}
					resolve(result.data)
				},
				fail: res => {
					reject(result.data)
				},
				complete() {
					loading && uni.hideLoading()
				}
			})

		})
	} else {
		data.hasOwnProperty('is_need_token') && (delete data.is_need_token, delete data.token)
		// 不验证token
		return new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + url,
				data: data,
				method: 'GET',
				header: {
					'content-type': 'application/x-www-form-urlencoded'
				},
				success: result => {
					uni.hideLoading();
					if (result.statusCode != 200) {
						uni.showModal({
							title: '错误警告',
							content: `出现${result.statusCode}错误`,
							showCancel: false
						})
						return
					}
					resolve(result.data)
				},
				fail: result => {
					uni.hideLoading();
					reject(result.data)
				}
			})

		})
	}
}
// post 请求
export const postRequest = (url, data = {}, blon = true, loading = true) => {
	if (loading) {
		uni.showLoading({
			title: "loading",
			mask: true
		})
	}
	if (blon) {
		data.token = uni.getStorageSync('token')
		// 验证token
		return new Promise((resolve, reject) => {
			data.hasOwnProperty('is_need_token') && (delete data.is_need_token,
				delete data.token)
			uni.request({
				url: baseUrl + url,
				data: data,
				method: 'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'token': data.token
				},
				success: result => {
					if (result.statusCode != 200) {
						uni.showModal({
							title: '错误警告',
							content: `出现${result.statusCode}错误`,
							showCancel: false
						})
						return
					}
					if (result.data.code == 10003) {
						// uni.clearStorage()
						let pathArr = getCurrentPages();
						let nowPath = pathArr[pathArr.length - 1].route
						if (routerArr.includes(nowPath)) {
							return
						}
						uni.showModal({
							title: "登录",
							content: "您还未登录或登录已过期,请重新登录~",
							showCancel: true,
							success: res => {
								if (res.confirm) {
									uni.navigateTo({
										url: loginUrl
									})
								} else {
									let path = getCurrentPages()
									if (path.length > 1) {
										uni.navigateBack({
											delta: 1,
											fail(error) {
												uni.reLaunch({
													url: '/pages/index/index'
												})
											}
										})
										// uni.navigateBack()
									} else {
										uni.reLaunch({
											url: '/pages/index/index'
										})
									}
								}
							}

						})
					}
					resolve(result.data)
				},
				fail: res => {
					reject(result.data)
				},
				complete() {
					loading && uni.hideLoading()
				}
			})
		})
	} else {
		data.hasOwnProperty('is_need_token') && (delete data.is_need_token, delete data.token)
		// 不验证token
		return new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + url,
				data: data,
				method: 'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded'
				},
				success: result => {
					uni.hideLoading();
					if (result.statusCode != 200) {
						uni.showModal({
							title: '错误警告',
							content: `出现${result.statusCode}错误`,
							showCancel: false
						})
						return
					}
					resolve(result.data)
				},
				fail: result => {
					uni.hideLoading();
					reject(result.data)
				}
			})

		})
	}
}
// post 请求
export const postRequest2 = (url, data = {}, blon = true, loading = true) => {
	if (loading) {
		uni.showLoading({
			title: "loading",
			mask: true
		})
	}
	if (blon) {
		data.token = uni.getStorageSync('token')
		// 验证token
		return new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + tokenApi,
				data: data,
				method: 'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'token': data.token
				},
				success: res => {
					if (res.statusCode != 200) {
						uni.showModal({
							title: '错误警告',
							content: `出现${res.statusCode}错误`,
							showCancel: false
						})
						return
					}
					if (res.data.code == 1) {
						data.hasOwnProperty('is_need_token') && (delete data.is_need_token,
							delete data.token)
						uni.request({
							url: baseUrl + url,
							data: data,
							method: 'POST',
							header: {
								'content-type': 'application/json; charset=utf-8',
								'token': data.token
							},
							success: result => {
								if (result.statusCode != 200) {
									uni.showModal({
										title: '错误警告',
										content: `出现${result.statusCode}错误`,
										showCancel: false
									})
									return
								}
								resolve(result.data)
							},
							fail: res => {
								reject(result.data)
							}
						})
					} else {
						uni.clearStorage()
						let pathArr = getCurrentPages();
						let nowPath = pathArr[pathArr.length - 1].route
						if (routerArr.includes(nowPath)) {
							return
						}
						uni.showModal({
							title: "登录",
							content: "您还未登录或登录已过期,请重新登录~",
							showCancel: true,
							success: res => {
								if (res.confirm) {
									uni.navigateTo({
										url: loginUrl
									})
								} else {
									let path = getCurrentPages()
									if (path.length > 1) {
										uni.navigateBack({
											delta: 1,
											fail(error) {
												uni.reLaunch({
													url: '/pages/index/index'
												})
											}
										})
										// uni.navigateBack()
									} else {
										uni.reLaunch({
											url: '/pages/index/index'
										})
									}
								}
							}

						})
					}
				},
				fail: res => {
					reject(res.data)
				},
				complete() {
					loading && uni.hideLoading()
				}
			})

		})
	} else {
		data.hasOwnProperty('is_need_token') && (delete data.is_need_token, delete data.token)
		// 不验证token
		return new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + url,
				data: data,
				method: 'POST',
				header: {
					'content-type': 'application/json; charset=utf-8',
				},
				success: result => {
					uni.hideLoading();
					if (result.statusCode != 200) {
						uni.showModal({
							title: '错误警告',
							content: `出现${result.statusCode}错误`,
							showCancel: false
						})
						return
					}
					resolve(result.data)
				},
				fail: result => {
					uni.hideLoading();
					reject(result.data)
				}
			})

		})
	}


}